package logic

import (
	"context"
	"shop_ppl/order_srv/order"

	"shop_ppl/api/internal/svc"
	"shop_ppl/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateInLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateInLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateInLogic {
	return &CreateInLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateInLogic) CreateIn(req *types.CreateInRequest) (resp *types.Response, err error) {
	goods, err := l.svcCtx.OrderSrv.CreateOrder(l.ctx, &order.CreateOrderRequest{
		OrderTitle: req.GoodsTitle,
		OrderNum:   req.GoodsNum,
		OrderPrice: req.GOOdsPrice,
	})
	if err != nil {
		return &types.Response{
			Come: -2,
			Msg:  "商品不存在",
			Data: nil,
		}, nil
	}
	if goods.Id == 0 {
		return &types.Response{
			Come: -1,
			Msg:  "库存不足",
			Data: nil,
		}, nil
	}
	if err != nil {
		return &types.Response{
			Come: -4,
			Msg:  "服务器内部错误",
			Data: nil,
		}, nil
	}
	return
}
